Skip to content

Speed up Find All References by avoiding unnecessary type inference - #23744

Closed
welf wants to merge 1 commit into
rust-lang:masterfrom
welf:avoid-eager-inference-in-reference-search
Closed

welf wants to merge 1 commit into
rust-lang:masterfrom
welf:avoid-eager-inference-in-reference-search

Conversation

@welf

@welf welf commented Nov 30, 2026

Copy link
Copy Markdown

Find All References starts with a search for matching names. When looking for a module named config, rust-analyzer can also encounter unrelated locals, fields, and methods named config. Classifying those candidates can trigger type inference for entire function bodies. That work can dominate the first reference search after indexing, when those bodies have yet to be inferred.

This change skips candidates whose syntax rules out a module reference. It also uses lexical resolution for path qualifiers, confirmed local variables, and bare identifier patterns. Paths that need type information, such as type-dependent associated items, still use the inference-based resolver. The change is in rust-analyzer’s reference search and applies to any editor that uses it.

Zed benchmark

On rust-analyzer’s own repository, the first Find All References request for the config module took a median of 769 ms before the patch and 80 ms after it, an 89.6% reduction. All ten completed requests returned the same 21 locations, with identical file URIs and ranges, including the declaration.

Run Before After
1 963 ms 82 ms
2 707 ms 80 ms
3 769 ms 94 ms
4 4,180 ms 76 ms
5 748 ms 64 ms

Measured in Zed Preview 1.31.0 on an Apple M5 Max. The runs alternated between binaries and used a fresh server process for each request. The timer covers textDocument/references from dispatch to response, after indexing and before the editor renders the results. One extra baseline attempt was interrupted by an early server restart. Its completed repeat is run 4.

Reproduce in Zed

  1. Build 9c4c59a4c6e0 and cce8b9bcc163 with cargo +1.99.0 build --locked --release -p rust-analyzer --bin rust-analyzer. Save the binaries at separate paths.
  2. Open the repository at 9c4c59a4c6e0 for both variants. Select the binary with lsp.rust-analyzer.binary.path in the project’s Zed settings. Set RUSTUP_TOOLCHAIN to 1.99.0 for the server and set checkOnSave to false in its initialization options. Zed Rust settings
  3. Restart the language server and wait for indexing to finish. Place the caret on config in the outer pub mod config; declaration at line 61, column 10. Run Find All References once from the command palette. Source location
  4. Alternate binaries for five runs each. For the numbers above, time the matching request and response in an LSP trace and compare the returned locations.

Regression coverage

test_find_all_refs_decl_module now checks a module that shares its name with a local binding, a field, and a method. It verifies that the import and the qualified use inside a function are still reported. Coverage marks confirm that the two candidate rejection paths are exercised. All 1,347 IDE library tests passed with cargo +1.99.0 test --locked --release -p ide --lib.

Codex assisted with profiling, the patch, the regression test, and the benchmark.

Resolve lexical paths and binding patterns without eagerly inferring their enclosing bodies. Filter field, method, and binding candidates during module reference searches.

AI-assisted-by: OpenAI Codex
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 30, 2026
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Is this PR text AI-written? Please read and follow the AI policy.

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Besides the PR text probably being AI-written,

  1. The code is excessively commented, a hallmark of AI-written code,
  2. We should anyway switch to lazy inference, which will alleviate everything here except modules,
  3. Modules are not perf-sensitive in search anyway probably but we may consider merging only their changes.

Which is why I'm closing this.

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 30, 2026
@welf

welf commented Nov 30, 2026

Copy link
Copy Markdown
Author

Is this PR text AI-written?

No, I wrote it but asked AI to improve its wording since I am not a native speaker.

The code is excessively commented, a hallmark of AI-written code

I always excessively document my code. It make the life much easier when you refactor it.

@ShoyuVanilla

Copy link
Copy Markdown
Member

All 1,347 IDE library tests passed with cargo +1.99.0 test --locked --release -p ide --lib.

You don't need to write this unless you are a LLM. We have CI after all. And actually, I could hardly belive that sentence is written by human because no one would run tests like cargo +$TOOLCHAIN test --locked --relese for no good reason. FWIW we use the latest stable toolchain (currently 1.100.1) for our development.

@welf

welf commented Nov 30, 2026

Copy link
Copy Markdown
Author

You don't need to write this unless you are a LLM. We have CI after all. And actually, I could hardly belive that sentence is written by human because no one would run tests like cargo +$TOOLCHAIN test --locked --relese for no good reason. FWIW we use the latest stable toolchain (currently 1.100.1) for our development.

I think you are a bit paranoic about AI. I think it is wrong but the repo is yours and you are free to set your own rules.

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

See #23746 for what I mean by "lazily" (module changes not included).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants